import 'dart:convert';
import 'package:fate_blind_box/routes/page_route_names.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/ui/Home/activity/act_list_model.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:fate_blind_box/utils/loading.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../im/groupchat.dart';
import 'group_list_member_model.dart';
import 'group_promote_item_view.dart';
import 'group_promote_list_controller.dart';

class GroupPromoteListPage extends StatefulWidget {
  FuturePopular? itemModel;

  GroupPromoteListPage({this.itemModel, Key? key}) : super(key: key);

  @override
  State<GroupPromoteListPage> createState() => _GroupPromoteListPageState();
}

class _GroupPromoteListPageState extends State<GroupPromoteListPage> {
  GroupPromoteListController promoteListController =
      Get.put(GroupPromoteListController());

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
      title: '选择群',
      bgColor: ThemeColor.sendMsgColor,
      rightActions: [
        UnconstrainedBox(
          child: CupertinoButton(
            padding: EdgeInsets.zero,
            onPressed: () {
              GroupMemberListData choseDataModel = GroupMemberListData();
              for (GroupMemberListData element
                  in promoteListController.dataList) {
                if (element.memberEaseGroup!.isChose) {
                  choseDataModel = element;
                  break;
                }
              }
              if (choseDataModel.memberEaseGroup!.isChose == false) {
                toast('请选择群');
                return;
              }
              Map<String, dynamic> jsonMap = {};
              jsonMap['activityId'] = widget.itemModel?.activityId ?? '';
              jsonMap['actiTitle'] = widget.itemModel?.actiTitle ?? '';
              jsonMap['actiCover'] = widget.itemModel?.actiCover ?? '';
              jsonMap['actiTime'] = widget.itemModel?.actiTime ?? '';
              jsonMap['actiCost'] = widget.itemModel?.actiCost ?? '';
              jsonMap['actiAddr'] = widget.itemModel?.actiAddr ?? '';
              jsonMap['easename'] = widget.itemModel?.easename ?? '';
              String jsonStr = jsonEncode(jsonMap);
              Map<String, String> dataJsonMap = {'content': jsonStr};
              Get.to(
                  GroupChat(
                    choseDataModel.memberEaseGroup?.groupId ?? '0',
                    choseDataModel.memberEaseGroup?.owner ?? '',
                    isFromShareUrl: true,
                    dataMap: dataJsonMap,
                  ),
                  routeName: PageRouteNames.groupChatPage);
            },
            child: Container(
              margin: const EdgeInsets.only(right: 15),
              width: 54,
              height: 31,
              decoration: BoxDecoration(
                color: ThemeColor.appColor,
                borderRadius: BorderRadius.circular(5),
              ),
              alignment: Alignment.center,
              child: const Text(
                '完成',
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
          ),
        ),
      ],
      childWidget: GetBuilder(
          id: promoteListController.groupListViewId,
          builder: (GroupPromoteListController controller) {
            return ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                GroupMemberListData dataModel = controller.dataList[index];
                return GetBuilder(
                    id: controller.groupListViewItemId,
                    builder: (GroupPromoteListController groupController) {
                      return GroupPromoteItemView(
                        count: dataModel.count ?? 0,
                        itemModel: dataModel.memberEaseGroup,
                        choseAction: () {
                          if (dataModel.memberEaseGroup!.isChose) {
                            return;
                          }
                          for (GroupMemberListData element
                              in controller.dataList) {
                            if (element.memberEaseGroup?.groupId ==
                                dataModel.memberEaseGroup?.groupId) {
                              element.memberEaseGroup?.isChose = true;
                            } else {
                              element.memberEaseGroup?.isChose = false;
                            }
                          }
                          groupController
                              .update([groupController.groupListViewItemId]);
                        },
                      );
                    });
              },
              itemCount: controller.dataList.length,
            );
          }),
    );
  }

  @override
  void initState() {
    super.initState();
    promoteListController.getData(widget.itemModel?.easename ?? '');
  }
}
